package com.delaphone.flocash.web.action;

import com.delaphone.flocash.web.bean.UserProfile;
import com.delaphone.flocash.service.AccountServiceImp;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.web.context.ServletContextAware;
import org.apache.log4j.Logger;

/**
 *
 * @author dongtv
 */
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {

    protected HttpSession session;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected ServletContext context;
    protected Logger logger = Logger.getLogger(this.getClass().getSimpleName());
    protected UserProfile user;

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        try {
            session = request.getSession(true);
            user = (UserProfile) session.getAttribute("user");
            if (user != null) {
                AccountServiceImp acc = new AccountServiceImp();
                user.setBalance(acc.getAccountBalance(user.getAccount()));
            }
        } catch (Exception ex) {
            logger.error("Create session error:", ex);
        }
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setServletContext(ServletContext context) {
        this.context = context;
    }
}
